package com.qf.confirm;

import com.qf.utils.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

/**
 * @author 郗昊天
 * @version 1.0
 * @package com.qf.simple
 * @date 2022/10/14 20:34
 */
public class Sender {

    private static final String EXCHANGENAME="xhtTopicChange";
    public static void main(String[] args) throws Exception {
        //获取连接
        Connection connection = ConnectionUtils.getconnection();
        //创建通道
        Channel channel = connection.createChannel();
        //声明交换机
        channel.exchangeDeclare(EXCHANGENAME, "topic");
        //开启发布确认
        channel.confirmSelect();
        //发送消息
        String msg="贺小倩喊你回家吃饭";

        for (int i = 0; i < 50; i++) {
            channel.basicPublish(EXCHANGENAME, "user.news", null, (msg+i).getBytes());
        }
        //等待服务器返回响应结果  如果为true则发送成功 只能确定消息到达了交换机  不能确认是否有队列接收并保存了消息
     //   boolean confirms = channel.waitForConfirms(); 只能用于一条消息
        channel.waitForConfirmsOrDie();//  用于批量接收数据  无返回结果 一个失败都失败 全部成功才成功 失败会抛异常
//        if(confirms){
//            System.out.println("发送成功");
//        }else {
//            System.out.println("发送失败");
//        }
        //用不到就是关闭
        channel.close();
        connection.close();

    }
}
